package com.asset.web.asset;

import com.asset.pojo.Asset;
import com.asset.service.AssetService;
import com.asset.util.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 11725
 */
@Slf4j
@RestController
@RequestMapping("/asset")
public class AssetController {

    @Resource
    private AssetService assetService;

    /**
     * 根据条件获取匹配的资产列表
     * @param page          当前页码
     * @param rows          每页记录条数
     * @param name          资产名称关键字
     * @param cid           可选，分类 id
     * @param depaId        可选，部门 id
     * @return              匹配的资产信息列表
     */
    @PreAuthorize("hasRole('USER')")
    @GetMapping("/list")
    public ResponseEntity<PageResult<Asset>> selectByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows,
            @RequestParam(value = "keyWord", defaultValue = "") String name,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "depaId", required = false) Long depaId
    ) {
        log.debug("cid: {}, depaId: {}, keyWord: {}", cid, depaId, name);
        return ResponseEntity.ok(assetService.selectByPage(page, rows, name, cid, depaId));
    }

    /**
     * 根据 id 查询资产详情
     * @param id            资产编号
     * @return              Asset 对象
     */
    @PreAuthorize("hasRole('USER')")
    @GetMapping("/{id}")
    public ResponseEntity<Asset> selectById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(assetService.selectById(id));
    }

    /**
     * 新增资产信息
     * @param asset         Asset 对象
     * @return              void - 201
     */
    @PreAuthorize("hasRole('MANAGE')")
    @PostMapping
    public ResponseEntity<Void> insert(@Valid @RequestBody Asset asset) {
        assetService.insert(asset);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改资产信息
     * @param asset         Asset 对象
     * @return              204 响应
     */
    @PreAuthorize("hasRole('MANAGE')")
    @PutMapping
    public ResponseEntity<Void> update(@Valid @RequestBody Asset asset) {
        assetService.update(asset);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除资产信息
     * @param id            资产编号
     * @return              204 响应
     */
    @PreAuthorize("hasRole('MANAGE')")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        assetService.delete(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 资产预定 & 从其他状态还原为闲置
     * - 闲置 -> 预定
     * - 预定/维护/维修 -> 闲置
     * @param id            资产编号
     * @param to            将要变更的状态
     * @param day           资产预定天数
     * @return              204 响应
     */
    @PreAuthorize("hasRole('USER')")
    @PutMapping("/{id}")
    public ResponseEntity<Void> changeStatus(
            @PathVariable("id") Long id,
            @RequestParam("to") Integer to,
            @RequestParam(value = "day", required = false) Integer day) {
        assetService.changeStatus(id, to, day);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据部门、资产状态查询匹配的所有资产信息
     * @param depaId        部门编号
     * @param status        资产状态
     * @return              List
     */
    @PreAuthorize("hasRole('USER')")
    @GetMapping("/list/all")
    public ResponseEntity<List<Asset>> selectByDepaIdAndStatus(
            @RequestParam("depaId") Long depaId,
            @RequestParam("status") Integer status) {
        List<Asset> list = assetService.selectByDepaIdAndStatus(depaId, status);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据用户 id 查询资产信息
     * @param id            用户 id
     * @return
     */
    @PreAuthorize("hasRole('USER')")
    @GetMapping("/user/{id}")
    public ResponseEntity<List<Asset>> selectByUserId(@PathVariable("id") Long id) {
        List<Asset> list = assetService.selectByUserId(id);
        return ResponseEntity.ok(list);
    }
}
